package com.ruoyi.system.service.impl.biz;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.biz.BmMemberReport;
import com.ruoyi.system.mapper.biz.BmMemberReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.biz.BmMemberAmountRecordMapper;
import com.ruoyi.system.domain.biz.BmMemberAmountRecord;
import com.ruoyi.system.service.biz.IBmMemberAmountRecordService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 会员缴费记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-11-18
 */
@Service
public class BmMemberAmountRecordServiceImpl implements IBmMemberAmountRecordService 
{
    @Autowired
    private BmMemberAmountRecordMapper bmMemberAmountRecordMapper;

    @Autowired
    private BmMemberReportMapper bmMemberReportMapper;

    /**
     * 查询会员缴费记录
     * 
     * @param id 会员缴费记录主键
     * @return 会员缴费记录
     */
    @Override
    public BmMemberAmountRecord selectBmMemberAmountRecordById(String id)
    {
        return bmMemberAmountRecordMapper.selectBmMemberAmountRecordById(id);
    }

    /**
     * 查询会员缴费记录列表
     * 
     * @param bmMemberAmountRecord 会员缴费记录
     * @return 会员缴费记录
     */
    @Override
    public List<BmMemberAmountRecord> selectBmMemberAmountRecordList(BmMemberAmountRecord bmMemberAmountRecord)
    {
        return bmMemberAmountRecordMapper.selectBmMemberAmountRecordList(bmMemberAmountRecord);
    }

    /**
     * 新增会员缴费记录
     * 
     * @param bmMemberAmountRecord 会员缴费记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertBmMemberAmountRecord(BmMemberAmountRecord bmMemberAmountRecord)
    {
        bmMemberAmountRecord.setCreateTime(DateUtils.getNowDate());
        //处理统计数据
        BmMemberReport bmMemberReport= bmMemberReportMapper.selectBmMemberReportByUserAccount(bmMemberAmountRecord.getAccount());
        if(null == bmMemberReport){
            bmMemberReport= new BmMemberReport();
            bmMemberReport.setSurplus(bmMemberAmountRecord.getAmount());
            bmMemberReport.setCosted(0l);
            bmMemberReport.setUserAccount(bmMemberAmountRecord.getAccount());
            bmMemberReportMapper.insertBmMemberReport(bmMemberReport);
        }else{
            bmMemberReport.setSurplus(bmMemberReport.getSurplus()+bmMemberAmountRecord.getAmount());
            bmMemberReportMapper.updateBmMemberReport(bmMemberReport);
        }

        return bmMemberAmountRecordMapper.insertBmMemberAmountRecord(bmMemberAmountRecord);
    }

    /**
     * 修改会员缴费记录
     * 
     * @param bmMemberAmountRecord 会员缴费记录
     * @return 结果
     */
    @Override
    public int updateBmMemberAmountRecord(BmMemberAmountRecord bmMemberAmountRecord)
    {
        bmMemberAmountRecord.setUpdateTime(DateUtils.getNowDate());
        return bmMemberAmountRecordMapper.updateBmMemberAmountRecord(bmMemberAmountRecord);
    }

    /**
     * 批量删除会员缴费记录
     * 
     * @param ids 需要删除的会员缴费记录主键
     * @return 结果
     */
    @Override
    public int deleteBmMemberAmountRecordByIds(String ids)
    {
        return bmMemberAmountRecordMapper.deleteBmMemberAmountRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除会员缴费记录信息
     * 
     * @param id 会员缴费记录主键
     * @return 结果
     */
    @Override
    public int deleteBmMemberAmountRecordById(String id)
    {
        return bmMemberAmountRecordMapper.deleteBmMemberAmountRecordById(id);
    }
}
